package com.wxlsoft.excel.convert.listeners;

import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JProgressBar;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.util.StringUtils;
import com.wxlsoft.excel.convert.model.IndexPackage;
import com.wxlsoft.excel.convert.model.IndexRecord;
import com.wxlsoft.excel.convert.model.IndexTo;
import com.wxlsoft.excel.convert.model.IndexTo.IndexNote;
import com.wxlsoft.excel.convert.model.SheetDataConvertCollector;
import com.wxlsoft.excel.core.AbstractSheetListener;

import lombok.NonNull;

/**
 * Index 监听页
 * @author wangxiaoliang
 */
public class IndexListener extends AbstractSheetListener<IndexRecord> {

	private final IndexPackage ip = new IndexPackage();
	
	private final SheetDataConvertCollector collector;
	
	public IndexListener(String sheetName, JProgressBar progress,
			@NonNull final SheetDataConvertCollector collector) {
		super(sheetName, progress);
		this.collector = collector;
	}
	
	@SuppressWarnings("deprecation")
	@Override
    public void invoke(IndexRecord data, AnalysisContext context) {
		Integer rowNum = context.getCurrentRowNum();
		data.setIndex(String.format("%03d", rowNum));
		
		if (isIndex(data)) {
			ip.addIndexTo(IndexConvert.toIndexto(data));
		} else if (isNote(data)) {
			ip.addNote(new IndexNote(data.getTradeCode()));			
		}
    }

	@Override
	protected void doAfter(AnalysisContext context) {
		collector.setIndexPackage(ip);
	}	
	
    public static class IndexConvert {

    	final static Pattern compile = Pattern.compile("(.*)\\((.*)\\)$");
    	
		/**
		 * 转输出 IndexTo
		 * @param data
		 * @return
		 */
		public static IndexTo toIndexto(IndexRecord from) {
			checkIndex(from);
			
			IndexTo to = new IndexTo();
			to.setInterfaceId(String.format("%s%s", from.getProvider(), from.getIndex()));
			to.setTradeCode(from.getTradeCode());
			to.setTradeName(from.getTradeName());
			
			Matcher matcher = compile.matcher(Objects.isNull(from.getServiceName()) ? "" : from.getServiceName());
			if (matcher.matches()) {
				to.setServiceName(matcher.group(1));
				to.setServiceCode(matcher.group(2));
			} 
			
			to.setSceneCode(from.getSceneCode());
			to.setSceneName(from.getSceneName());
			to.setConsume(from.getConsume());
			to.setProvider(from.getProvider());
			to.setDirection(from.getDirection());
			to.setProviderSystemId(from.getProviderSystemId());
			to.setDataBodyLayout(from.getDataBodyLayout());
			to.setProcessor(from.getProcessor());
			to.setUpdateTime(from.getUpdateTime());
			to.setDataBodyDirection(from.getDataBodyDirection());
			to.setConsumeSystemName(from.getConsumeSystemName());
			to.setDocument(from.getDocument());
			to.setIsPenetrate(from.getIsPenetrate());
			to.setDataHeader(from.getDataHeader());
			to.setInterfaceStatus(from.getInterfaceStatus());
			to.setSceneStatus(from.getSceneStatus());
			to.setIsStandard(from.getIsStandard());
			return to;
		}
		
		public static void checkIndex(IndexRecord from) {
			String isPenetrate = from.getIsPenetrate();
			String isStandard = from.getIsStandard();
			
			if (Objects.isNull(isPenetrate) 
					|| Objects.isNull(isStandard)) {
				throw new RuntimeException(String.format("第%s行，数据格式错误，请检查: %s, %s", from.getIndex(), from.getTradeCode()));
			}
		}
		
    }
    
    private boolean isNote(IndexRecord data) {
    	String tradeCode = data.getTradeCode();
		return StringUtils.isNotBlank(tradeCode);
	}

	/**
	 * 是数字
	 * @param data
	 * @return
	 */
	private boolean isIndex(IndexRecord data) {
		String tradeCode = data.getTradeCode();
		String tradeName = data.getTradeName();
		return StringUtils.isNotBlank(tradeCode)
				&& StringUtils.isNotBlank(tradeName);
	}

}
